<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>printer.c</title>
<style type="text/css">
<!--
body.hl	{ background-color:#ffffff; }
pre.hl	{ color:#000000; background-color:#ffffff; font-size:10pt; font-family:'Courier New';}
.hl.num { color:#ff0000; }
.hl.esc { color:#ff22ff; }
.hl.str { color:#ff0000; }
.hl.dstr { color:#ff0000; }
.hl.slc { color:#0000ff; }
.hl.com { color:#0000ff; }
.hl.dir { color:#ff22ff; }
.hl.sym { color:#000000; }
.hl.line { color:#0000ff; }
.hl.mark	{ background-color:#ffffbb;}
.hl.kwa { color:#b26818; }
.hl.kwb { color:#00ff00; }
.hl.kwc { color:#b26818; }
.hl.kwd { color:#000000; }
//-->
</style>
</head>
<body class="hl">
<pre class="hl"><span class="hl com">/*</span>
<span class="hl com"> *  printer.c</span>
<span class="hl com"> *  scheme2</span>
<span class="hl com"> *</span>
<span class="hl com"> *  Created by Leo Katzenstein on 5/26/07.</span>
<span class="hl com"> *  Copyright 2007 __MyCompanyName__. All rights reserved.</span>
<span class="hl com"> *</span>
<span class="hl com"> */</span>

<span class="hl dir">#include</span> <span class="hl dstr">&quot;scheme.h&quot;</span><span class="hl dir"></span>

<span class="hl kwb">static void</span> <span class="hl kwd">print_list</span><span class="hl sym">(</span> Pair <span class="hl sym">*</span>l<span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwa">while</span> <span class="hl sym">(</span> l <span class="hl sym">) {</span>
        <span class="hl kwd">pexp1</span><span class="hl sym">(</span>l<span class="hl sym">-&gt;</span>car<span class="hl sym">);</span>
        l <span class="hl sym">=</span> l<span class="hl sym">-&gt;</span>cdr<span class="hl sym">;</span>
        <span class="hl kwa">if</span> <span class="hl sym">(</span><span class="hl kwd">atom</span><span class="hl sym">(</span>l<span class="hl sym">)) {</span>
            <span class="hl kwa">if</span> <span class="hl sym">(</span> l <span class="hl sym">) {</span>
                <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot; . &quot;</span> <span class="hl sym">);</span>
                <span class="hl kwd">pexp1</span><span class="hl sym">(</span> l <span class="hl sym">);</span>
            <span class="hl sym">}</span>
            <span class="hl kwa">return</span><span class="hl sym">;</span>
        <span class="hl sym">}</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot; &quot;</span> <span class="hl sym">);</span>
    <span class="hl sym">}</span>
    <span class="hl kwd">error</span><span class="hl sym">(</span> <span class="hl str">&quot;print_list: called with null&quot;</span> <span class="hl sym">);</span>
<span class="hl sym">}</span>

<span class="hl kwb">void</span> <span class="hl kwd">pexp1</span><span class="hl sym">(</span> Pair <span class="hl sym">*</span>e <span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwb">int</span> el<span class="hl sym">;</span>  <span class="hl com">/* environment level */</span>
    Pair <span class="hl sym">*</span>ep<span class="hl sym">;</span>

    <span class="hl kwa">if</span> <span class="hl sym">(</span> e <span class="hl sym">==</span> NULL <span class="hl sym">)</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;nil&quot;</span> <span class="hl sym">);</span>
    <span class="hl kwa">else if</span> <span class="hl sym">(</span> <span class="hl kwd">is_fixnum</span><span class="hl sym">(</span> e <span class="hl sym">) )</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;%d&quot;</span><span class="hl sym">,</span> <span class="hl kwd">nval</span><span class="hl sym">(</span>e<span class="hl sym">) );</span>
    <span class="hl kwa">else if</span> <span class="hl sym">(</span> <span class="hl kwd">is_double</span><span class="hl sym">(</span> e <span class="hl sym">) )</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;%f&quot;</span><span class="hl sym">,</span> <span class="hl kwd">dval</span><span class="hl sym">(</span>e<span class="hl sym">) );</span>
    <span class="hl kwa">else if</span> <span class="hl sym">(</span> <span class="hl kwd">is_symbol</span><span class="hl sym">(</span> e <span class="hl sym">) )</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span><span class="hl str">&quot;%s&quot;</span><span class="hl sym">,</span> <span class="hl kwd">pname</span><span class="hl sym">(</span>e<span class="hl sym">));</span>
    <span class="hl kwa">else if</span> <span class="hl sym">(</span> <span class="hl kwd">is_func</span><span class="hl sym">(</span> e <span class="hl sym">) )</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;&lt;compiled function(0x%x)&gt;&quot;</span><span class="hl sym">,</span> e<span class="hl sym">-&gt;</span>cdr <span class="hl sym">);</span>
    <span class="hl kwa">else if</span> <span class="hl sym">(</span> <span class="hl kwd">is_lambda</span><span class="hl sym">(</span>e<span class="hl sym">) ) {</span>
        <span class="hl kwa">for</span> <span class="hl sym">(</span> el<span class="hl sym">=</span><span class="hl num">0</span><span class="hl sym">,</span> ep<span class="hl sym">=</span><span class="hl kwd">CADR</span><span class="hl sym">(</span>e<span class="hl sym">);</span> ep<span class="hl sym">-&gt;</span>cdr<span class="hl sym">;</span> ep<span class="hl sym">=</span>ep<span class="hl sym">-&gt;</span>cdr <span class="hl sym">)</span>
            el <span class="hl sym">+=</span> <span class="hl num">1</span><span class="hl sym">;</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;(lambda env(%d) &quot;</span><span class="hl sym">,</span> el <span class="hl sym">);</span>

        <span class="hl kwd">print_list</span><span class="hl sym">(</span>e<span class="hl sym">-&gt;</span>cdr<span class="hl sym">-&gt;</span>cdr<span class="hl sym">);</span>
        <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;)</span><span class="hl esc">\n</span><span class="hl str">&quot;</span> <span class="hl sym">);</span>
    <span class="hl sym">}</span> <span class="hl kwa">else</span>
        <span class="hl sym">{</span>
            <span class="hl com">/* this is a non null list */</span>
            <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;(&quot;</span> <span class="hl sym">);</span>
            <span class="hl kwd">print_list</span><span class="hl sym">(</span> e <span class="hl sym">);</span>
            <span class="hl com">/*</span>
<span class="hl com">            pexp1( e-&gt;car );</span>
<span class="hl com">            for ( e = e-&gt;cdr; e; e = e-&gt;cdr )</span>
<span class="hl com">            {</span>
<span class="hl com">                printf( &quot; &quot; );</span>
<span class="hl com">                pexp1( e-&gt;car );</span>
<span class="hl com">            }</span>
<span class="hl com">            */</span>
            <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;)&quot;</span> <span class="hl sym">);</span>
        <span class="hl sym">}</span>
<span class="hl sym">}</span>

<span class="hl kwb">void</span> <span class="hl kwd">pexp</span><span class="hl sym">(</span> Pair <span class="hl sym">*</span>e <span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwd">pexp1</span><span class="hl sym">(</span> e <span class="hl sym">);</span>
    <span class="hl kwd">printf</span><span class="hl sym">(</span> <span class="hl str">&quot;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span> <span class="hl sym">);</span>
<span class="hl sym">}</span>
</pre>
</body>
</html>
<!--HTML generated by highlight 2.6.14, http://www.andre-simon.de/-->
